package com.mi.common.core.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Long Array serializer
 *
 * @author jin_huaquan
 * @version 1.0
 * @date 2022/1/11 16:28
 */
public class LongCollectionSerializer extends JsonSerializer<List<Long>> {

    @Override
    public void serialize(List<Long> longs, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (Objects.isNull(longs)) {
            jsonGenerator.writeArray(new String[0], 0, 0);
        }
        List<String> strings = longs.stream()
                .map(Object::toString)
                .collect(Collectors.toList());

        jsonGenerator.writeArray(strings.toArray(new String[0]), 0, strings.size());
    }
}
